<template>
    <el-dialog
            :close-on-click-modal="false"
            :model-value="api.editState.visible"
            :title="api.editState.title"
            destroy-on-close
            width="500"
            @close="api.onCloseEdit()"
    >
        <el-form
                ref="formRef"
                :model="api.editState.form"
                :rules="rules"
                label-width="80px"
                status-icon
        >
            <el-form-item
                    label="包路径"
                    prop="packageName"
            >
                <el-input
                        v-model="api.editState.form.packageName"
                        clearable
                        placeholder="包路径"
                />
            </el-form-item>

            <el-form-item
                    label="表名"
                    prop="tableName"
            >
                <el-input
                        v-model="api.editState.form.tableName"
                        clearable
                        placeholder="表名"
                />
            </el-form-item>

            <el-form-item
                    label="表描述"
                    prop="tableComment"
            >
                <el-input
                        v-model="api.editState.form.tableComment"
                        clearable
                        placeholder="表描述"
                />
            </el-form-item>

            <el-form-item
                    label="模块名"
                    prop="moduleName"
            >
                <el-input
                        v-model="api.editState.form.moduleName"
                        clearable
                        placeholder="模块名"
                />
            </el-form-item>

            <el-form-item
                    label="类名"
                    prop="className"
            >
                <el-input
                        v-model="api.editState.form.className"
                        clearable
                        placeholder="类名"
                />
            </el-form-item>

            <el-form-item
                    label="request"
                    prop="requestMapping"
            >
                <el-input
                        v-model="api.editState.form.requestMapping"
                        clearable
                        placeholder="requestMapping"
                />
            </el-form-item>

            <el-form-item class="mb-0 mt-4">
                <el-button type="primary" @click="onSubmit">保存</el-button>
                <el-button @click="api.onCloseEdit()">关闭</el-button>
            </el-form-item>
        </el-form>
    </el-dialog>
</template>

<script lang="ts" setup>
import {GenTableApi} from "~/api/system/gen/gen.api";
import {reactive, ref} from "vue";
import {FormRules} from "element-plus";

interface Props {
    api: GenTableApi;
}

const formRef = ref();
const {api} = withDefaults(defineProps<Props>(), {});
const rules = reactive<FormRules>({
    packageName: [{required: true, message: "请输入包路径", trigger: "blur"}],
    tableName: [{required: true, message: "请输入表名", trigger: "blur"}],
    tableComment: [{required: true, message: "请输入表描述", trigger: "blur"}],
    moduleName: [{required: true, message: "请输入模块名", trigger: "blur"}],
    className: [{required: true, message: "请输入类名", trigger: "blur"}],
    requestMapping: [{required: true, message: "请输入requestMapping", trigger: "blur"}],
});

function addField() {
    if (!api.editState.form.fields) {
        api.editState.form.fields = [];
    }
    api.editState.form.fields.push({
        type: "String",
        name: "test",
        exist: true,
        commentComment: "备注",
    });
}

async function onDeleteField(index: number) {
    api.createState.form.fields?.splice(index, 1);
}

async function onSubmit() {
    const values = await formRef.value.validate();
    if (values) {
        api.submit(api.editState.form).then((res: any) => {
            if (res.ok) {
                api.onCloseEdit();
            }
        });
    }
}
</script>

<style lang="scss" scoped>

</style>
